import {
    Base64
} from '../../common/base64.js'
import config from '../../common/config'
// pages/payment/payment.js
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let base64 = new Base64();
        let base64Str = options.info + "=";
        let jsonStr = base64.decode(base64Str);
        let info = JSON.parse(jsonStr);
        let orderno = '';
        //获取页面栈
        let pages = getCurrentPages();
        console.log(info);
        wx.requestPayment({
            'timeStamp': info.timeStamp,
            'nonceStr': info.nonceStr,
            'package': info.package,
            'signType': info.signType,
            'paySign': info.paySign,
            'success': () => {
                this.paymentSuccessHandler(info.orderNo);
            },
            'fail': (res) => {
                console.log(res);
                if (res.errMsg.indexOf('cancel') >= 0) {
                    this.showPayCancelMessage(res, info.orderNo);
                    return;
                }
                this.showPayFailMessage(res, info.orderNo);
            },
            'complete': function() {}
        });
    },
    /**
     * 支付失败的情况下弹出支付失败对话框
     */
    showPayFailMessage(res, orderNo) {
        wx.showModal({
            title: '支付失败',
            content: res.errMsg,
            showCancel: false,
            success: (res) => {
                let url = '../pakjh5/pakjh5?route=OrderPayment&orderno=' + orderNo + '&result=failed';
                wx.redirectTo({
                    url: url
                })
            }
        })
    },
    /**
     * 支付取消的情况下弹出支付失败对话框
     */
    showPayCancelMessage(res, orderNo) {
        let url = '../pakjh5/pakjh5?route=OrderPayment&orderno=' + orderNo + '&result=failed';
        wx.redirectTo({
            url: url
        })
    },
    /**
     * 支付成功以后返回相关web-view页面
     */
    paymentSuccessHandler(orderNo) {
        let url = '../pakjh5/pakjh5?route=OrderDetail&orderno=' + orderNo + '&result=success';
        wx.redirectTo({
            url: url
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})